/**
 * 计算SIGMA{SIGMA(max(Ai - Aj, 0))}
 * 固定i，则Ai对结果的贡献就是： kAi - Si
 * 其中k是i位置之前比Ai小的元素的数量，Si则是i位置之前比Ai小的元素之和
 * 因此维护两个树状数组即可，需要离散化
 */
#include <bits/stdc++.h>
using namespace std;

#include <bits/extc++.h>

using llt = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vll = vector<llt>;

template<typename T>
void input(vector<T> & a, int n){
    a.assign(n + 1, {});
    for(int i=1;i<=n;++i) cin >> a[i];
    return;
}

template<typename T>
istream & operator >> (istream & is, vector<T> & v){
    for(auto & i : v) is >> i;
    return is;
}

struct FenwickTree{ // 树状数组

using value_type = long long int;
using vec_type = vector<value_type>;

int n;
vec_type c;

FenwickTree() = default;

static int lowbit(int x){return x & -x;}

void init(int nn){this->c.assign((this->n=nn) + 1, 0);}

void modify(int pos, value_type delta){
    for(int i=pos;i<=this->n;i+=lowbit(i)) this->c[i] += delta;
}

value_type query(int pos)const{
    value_type ans = 0;
    for(int i=pos;i;i-=lowbit(i)) ans += this->c[i];
    return ans;
}

value_type query(int s, int e)const{return this->query(e) - this->query(s - 1);}

}Bt, Ct;

int N;
vi A;
vi B;

void work(){
    cin >> N;
    input(A, N);

    A[0] = 0;
    for(int i=1;i<=N;++i) A[i] += 1;
    
    B.assign(A.begin(), A.end());    
    sort(B.begin(), B.end());
    B.erase(unique(B.begin(), B.end()), B.end());

    Bt.init(B.size() + 1);
    Ct.init(B.size() + 1);

    llt ans = 0;
    for(int i=1;i<=N;++i){
        int c = A[i];
        int u = lower_bound(B.begin(), B.end(), c) - B.begin();

        llt tmp = Bt.query(u - 1);
        llt cnt = Ct.query(u - 1);
        ans += (c - 1) * cnt - tmp;
        Bt.modify(u, c - 1);
        Ct.modify(u, 1);
    }
    cout << ans << endl;
    return;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int nofkase = 1;
    // cin >> nofkase;
    while(nofkase--) work();
    return 0;
}